package com.winit.openapi.mks.controller;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.winit.common.query.Page;
import com.winit.common.spi.context.CommandContext;
import com.winit.mks.spi.gift.card.GiftCardService;
import com.winit.mks.spi.gift.card.command.GiftCardCommand;
import com.winit.mks.spi.gift.card.command.QueryGiftCardCommand;
import com.winit.mks.spi.gift.card.vo.GiftCardVo;
import com.winit.mks.spi.gift.log.GiftCardLogService;
import com.winit.mks.spi.gift.log.command.QueryGiftCardLogCommand;
import com.winit.mks.spi.gift.log.vo.GiftCardLogVo;
import com.winit.openapi.constants.ErrorCode;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.exception.ApiException;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.sms.common.SmsUtils;
import com.winit.sms.spi.common.vo.PageVo;
import com.winit.ums.spi.UmsCustomerService;
import com.winit.ums.spi.commnad.customer.QueryCompanylCommand;
import com.winit.ums.spi.vo.CustomerInfoVO;

/**
 * 
 * 礼品卡管理controller
 * 
 * @version 
 * <pre>
 * Author	Version		Date		Changes
 * song.zhou1 	1.0  		2017年9月12日 	Created
 *
 * </pre>
 * @since 1.
 */

@Controller
@RequestMapping("/mks/giftcard")
public class GiftCardController  extends BaseController {
    
    private static Logger   logger = LoggerFactory.getLogger(GiftCardController.class);
    
    @Resource
    private GiftCardService giftCardService;
    @Resource
    private GiftCardLogService giftCardLogService;
    @Resource
    private UmsCustomerService umsCustomerService;
    
    /**
     * 分页查询礼品卡列表
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/queryGiftCardPageList")
    public String queryGiftCardPageList(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        QueryGiftCardCommand command = SmsUtils.toJavaObject(requestMsg.getData(), QueryGiftCardCommand.class);
        CommandContext ctx = CommandContext.getContext();
        command.setCtx(ctx);
        //获取用户信息
        CustomerInfoVO customerInfo = this.getContextCustomerInfo();
        String customerCode = customerInfo.getCode();
        command.setCustomerCode(customerCode);
        PageVo pageVo = SmsUtils.SetPageParams(requestMsg.getData());
        command.setPageVo(pageVo);
        Page<GiftCardVo> pageList = null;
        try{
            pageList = giftCardService.queryGiftCardPage(command);
        }catch(Exception e){
            logger.error("GiftCardController-->queryGiftCardPageList error:"+command);
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        Map<String, Object> data = SmsUtils.setPageData(pageList);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 查看使用记录
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("/queryCardUseLog")
    public String queryCardUseLog(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        QueryGiftCardLogCommand command = SmsUtils.toJavaObject(requestMsg.getData(), QueryGiftCardLogCommand.class);
        CommandContext ctx = CommandContext.getContext();
        command.setCtx(ctx);
        PageVo pageVo = SmsUtils.SetPageParams(requestMsg.getData());
        command.setPageVo(pageVo);
        Page<GiftCardLogVo> pageList = null;
        try{
            pageList = giftCardLogService.queryGiftCardLogPage(command);
        }catch(Exception e){
            logger.error("GiftCardController-->queryCardUseLog error:"+command);
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        Map<String, Object> data = SmsUtils.setPageData(pageList);
        responseMsg.setData(data);
        return SUCCESS;
    }
    
    /**
     * 激活接口
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping("activateCard")
    public String activateCard(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg){
        GiftCardCommand command = SmsUtils.toJavaObject(requestMsg.getData(), GiftCardCommand.class);
        CommandContext ctx = CommandContext.getContext();
        command.setCtx(ctx);
        command.setCardStatus("CA");
        Long flag = 0L;
        try{
            flag = giftCardService.modifyGiftCardStatus(command);
        }catch(Exception e){
            logger.error("GiftCardController-->activateCard error:"+command);
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        if(flag > 0 ){
            return SUCCESS;
        }else{
            return ERROR;
        }
    }
    
    /** 从上下文中获取客户信息 */
    private CustomerInfoVO getContextCustomerInfo() {
        QueryCompanylCommand userCommand = new QueryCompanylCommand();
        userCommand.setEmail(CommandContext.getContext().getUsername());
        CustomerInfoVO customer = umsCustomerService.queryCompanyByUserName(userCommand);
        if(customer == null) {
            throw new ApiException(ErrorCode.SYSTEM_ERROR);
        }
        return customer;
    }
}
